# -*- coding: utf-8 -*-
# @Author  : 李吉凯
# @Email   : lijk34925@hundsum.com
# @Time    : 2021/5/9 20:04
# @File    : Log.py

import logging


def log_init(logFile, log_level='info'):
    log_level = log_level.upper()
    # 创建logger对象
    logger = logging.getLogger()
    # 创建一个handler，用于写入日志文件,文件输出流的对象
    fileHandler = logging.FileHandler(logFile, encoding='utf-8')
    # 再创建一个handler，用于输出控制台日志，“屏幕输出对象”
    streamHandler = logging.StreamHandler()
    # 创建标准流对象
    formatter = logging.Formatter('%(asctime)s %(filename)s %(lineno)d %(levelname)s : %(message)s')
    # 把日志格式给文件输出对象
    fileHandler.setFormatter(formatter)
    # 把日志格式给控制台输出对象
    streamHandler.setFormatter(formatter)
    # logger对象通过addHandler方法把文件输出对象和控制台输出对象加进来
    logger.addHandler(fileHandler)
    logger.addHandler(streamHandler)
    # 设置日志级别
    if log_level == 'WARN':
        level = logging.WARN
    elif log_level == 'ERROR':
        level = logging.ERROR
    elif log_level == 'DEBUG':
        level = logging.DEBUG
    else:
        level = logging.INFO
    logger.setLevel(level)
    return logger